; http://code.google.com/p/springer-loader/
; by Ritterbrater
; May 2011
; GNU GPL V3
; file created with notepad2

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.


Gui, Add, Text, x16 y29 w400 h15 , Links (copy from link: Browse This Book -> Contents):
Gui, Add, Edit, x16 y43 w400 h350 ,
Gui, Add, Button, x326 y413 w90 h30 , Go!
Gui, Add, Text, x16 y413 w290 h30 , By pressing OK you accept Terms of Service of Springerlink, HTTrack and SpringerLoader
; Generated using SmartGUI Creator 4.0
Gui, Show, x246 y195 h460 w436, SpringerLoader
Return

ButtonGo!:

ControlGetText, urls, Edit1
Gui, Destroy

startdownloading(urls)
;do sth
exitapp

GuiClose:
ExitApp
 

startdownloading(urls)
{
  SetWorkingDir %A_ScriptDir%

  iniread, par1, %a_scriptdir%\config.ini, Options, launchparameters1
  iniread, par2, %a_scriptdir%\config.ini, Options, launchparameters2

  iniread, showballoon, %a_scriptdir%\config.ini, Options2, showballoontip
  iniread, showfinished, %a_scriptdir%\config.ini, Options2, showfinishedbox
  iniread, par, %a_scriptdir%\config.ini, Options2, filelength
  if (par < 1)
  {
    msgbox Config.ini is corrupted or missing! `n Exit!
  }
  getfilelength(par) ;initialisieren der maximalen Dateinamenlänge
  
  urls := urls . "`n"
  
  anzurls := counturls(urls)
  showballoon(0, "Buch", anzurls, showballoon)
  
  loop
  {

  stringgetpos, pos, urls, `n
  if pos < 1 then
    break
  stringleft, url, urls, pos - 1
  stringtrimleft, urls, urls, pos + 1
 
  ;msgbox %url%
  url := getmainurl(url)
  ;msgbox %url%
  download(url, par1, par2)
  filegetsize, groe, %a_scriptdir%\index-2.html, k
  if groe < 10 ;Fehlerhaft!
  {
    filedelete, %a_workingdir%\index-2.html 
    filedelete, %a_workingdir%\index-2.html.readme 
    return
  }
  downloadfromparsing(par1, par2)
  }
  
  showballoon(-99, "Buch") ;Finished balloon tip

  if(!showfinished && showballoon)
    sleep, 5005
  
  if (showfinished)
    msgbox Finished!
exitapp
}
;---

downloadfromparsing(par1, par2)
{
  ;constants
  fname1 := "fulltext.pdf"
  fname2 := "front-matter.pdf"
  fname3 := "back-matter.pdf"

  searchstring1:="fulltext.pdf"
  searchstring2:="front-matter.pdf""" . " "
  searchstring3:="back-matter.pdf""" . " "

  specialtitle2 := "Front_matter"
  specialtitle3 := "Back_matter"

  static anzfinished = 0
  
  ;stringgetpos, pos, url, /, R2
  ;stringleft, url2, url, pos
  ;msgbox, %url2%

  ;FileSelectFile, file
  file := a_scriptdir . "\index-2.html" ;es gibt keine index.html
  ;msgbox testhalt
  booktitle := extracttitle(file)
  if strlen(booktitle) < 4
    return
    
  
  destfolder := trycreatefolder(booktitle)

  ;cover image
  cover := extractcover(file)
  title := "http://www.springerlink.com" . cover
  download(title, par1, par2)
  trymovefile(destfolder, "cover-large.gif", "cover.gif", 98)
  

    i = 1
    ;"/content/jq516w7924701460/fulltext.pdf"
    Loop, read, %file%
    {
      line := A_LoopReadLine
      

      loop, 3 ; %a_index% from 1 to 3
      {
      
        if (instr(line, searchstring%a_index%) > 0)
        {

          ;URL rausfischen
          ftempname := fname%a_index%  ;workaround for next line
          stringgetpos, pos, line, %ftempname%, R
          stringleft, title, line, pos + strlen(ftempname)
          stringgetpos, pos, title, ", R
          stringright, title, title, strlen(title) - pos-1
          fturl := "http://www.springerlink.com" . title
          
          ;urls0x%i% := title
          
          ;msgbox %title% `n %oldline%
                
          ;Kapitelname
          
          if (a_index = 1) ;only for real chapters
          {
            stringgetpos, pos, oldline, </a>
            stringgetpos, posspan, oldline, <span class="subtitle">
            ;Bug; manchmal sind die Kapitelnamen leer, dann nach nächsten '<' suchen
            if ((posspan < 10) && (posspan > -1)) ;-1 ~ not found
            {
              stringgetpos, pos, oldoldline, <, R
              oldline := oldoldline
            }
            stringleft, title, oldline, pos
            stringgetpos, pos, title, >, R
            stringright, title, title, strlen(title) - pos-1
            ftname := correctfile(title)
          }
          else
            ftname := specialtitle%a_index%   ;for front and back matter

          ;msgbox, %fturl% %a_index%

          download(fturl, par1, par2)
          trymovefile(destfolder, ftempname, ftname . ".pdf", i)

          ;urls1x%i% := title
          
          ;msgbox %title%
          i := i + 1
        }

        
      }
      
      ;remind old read lines for chapter names
      oldoldline := oldline ;Bug, wenn Unterkapitelnamen; Beispiel: http://www.springerlink.de/content/978-3-8348-0172-2/contents/
                                                                  ;attention: url must be updated via browser
      oldline := A_LoopReadLine
    }
   

  ;zuletzt noch Index verschieben

  trymovefile(destfolder, "index-2.html", "about.html", 99)
  
  anzfinished := anzfinished + 1
  
  showballoon(anzfinished, booktitle)

  return
}
;----------------------

download(url, par1, par2)
{
  url = %url%
  cmd:="httrack " . par1 . " " . url . " " . par2
  ;msgbox, %cmd%
  runwait, %cmd%,,hide
  ;msgbox "start"
  sleep, 500
  ;msgbox sleep
}

getmainurl(url)
{
  stringgetpos, pos, url, doi.org
  if pos > 0 then
  {
    stringgetpos, urlpos, url, /, R
    if strlen(url) - urlpos = 1
      stringleft, url, url, strlen(url) - 1
    stringgetpos, urlpos, url, /, R
    stringright, url2, url, strlen(url) - urlpos
    url2 := "http://www.springerlink.com/content" . url2
  }  
  else
  {
    stringgetpos, urlpos, url, content/
    urlpos := urlpos + 9 ;offset for word content/
    stringgetpos, urlpos, url, /,, urlpos
    stringleft, url2, url, urlpos
  }
  url2 := url2 . "/contents/#AboutSection"
  
  return url2
}

extracttitle(file)
{
  i = 0
  ;<dt>Title</dt>
  Loop, read, %file%
  {
    if (instr(A_LoopReadLine, "<dt>Title</dt>") > 0)
    {
      line := a_loopreadline
      break
    }
  }
  
  stringgetpos, pos, line, <dt>Title</dt>
  stringright, title, line, strlen(line) - pos - strlen("<dt>Title</dt>")
  
  
  loop
  {
    stringgetpos, pos, title, >
    if pos < 5
      stringright, title, line, strlen(title) - pos - 1
    else
      break
  }
  
  stringgetpos, pos, title, <
  stringleft, title, title, pos
  
  title := correctfile(title)
  
  ;msgbox, %title%
  return title
}

correctfile(title) ;must be changed again one day....
{
  ;http://de.autohotkey.com/forum/post-52193.html&sid=bdee8fd290daaebc43c34bf2ce147aa6#52193
  a = ,
  StringReplace, title, title, %A_SPACE%, _, All
  StringReplace, title, title, %A_Tab%, _, All
  StringReplace, title, title, Ã¤, ae, All
  StringReplace, title, title, Ã, Ae, All
  StringReplace, title, title, Ã¼, ue, All
  StringReplace, title, title, Ã, Ue, All
  StringReplace, title, title, Ã¶, oe, All
  StringReplace, title, title, Ã, Oe, All
  
  StringReplace, title, title, Ã, ss, All
  StringReplace, title, title, %a%, , All ;kill comma
  
  ;even more problems: http://en.wikipedia.org/wiki/Filename
  a = :
  StringReplace, title, title, %a%, _, All
  a = \
  StringReplace, title, title, %a%, _, All
  a = /
  StringReplace, title, title, %a%, _, All
  a = ?
  StringReplace, title, title, %a%, _, All
  a = `%
  StringReplace, title, title, %a%, _, All
  a = *
  StringReplace, title, title, %a%, _, All
  a = |
  StringReplace, title, title, %a%, _, All
  a = ""
  StringReplace, title, title, %a%, _, All
  a = <
  StringReplace, title, title, %a%, _, All
  a = >
  StringReplace, title, title, %a%, _, All
  a = .
  StringReplace, title, title, %a%, _, All
  
  len := getfilelength() 
  stringleft, title, title, %len%
  return title
}

extractcover(file)
{
  i = 0
  ;(/content/u6l5p0/cover-medium.gif)
  Loop, read, %file%
  {
    if (instr(A_LoopReadLine, "cover-") > 0)
    {
      line := a_loopreadline
      break
    }
  }

  stringgetpos, pos, line, ), R
  stringleft, title, line, pos
  stringgetpos, pos, title, (, R
  stringright, title, title, strlen(title) - pos-1
  
  ;even large cover is availible; download it!
  stringleft, title, title, strlen(title) - strlen("medium.gif")
  title := title . "large.gif"



  ;title := correctfile(title)
  
  return title
  
}

trycreatefolder(name)
{
  name := A_ScriptDir . "\" . name
  name2 := name
  i := 0
  if !FileExist(name2)
    filecreatedir, %name2%
  else
  {
    i := 0
    loop
    {
      i := i + 1
      
      if i < 10 ;zweistellig sollte reichen
        name2 := name . "_0" . i
      else
        name2 := name . i
        
      if !FileExist(name2)
      {
        filecreatedir, %name2%
        break
      }
    }
  }
    
  return name2
}

trymovefile(path, sourcename, name, number)
{
  if number < 10 ;zweistellig sollte reichen
    name2 := "0" . number
  else
    name2 := number
  filemove, %a_workingdir%\%sourcename%, %path%\%name2%_%name%
  filedelete, %a_workingdir%\%sourcename%.readme ;Restmüll von httrack entfernen
}

getfilelength(len = 0)
{
  static length = 0
  if (len <> 0)
    length := len
  return length
}

counturls(urls)
{
  i := 0
  loop
  {
    stringgetpos, pos, urls, `n
    if pos < 1 then
      break
    stringleft, url, urls, pos - 1
    stringtrimleft, urls, urls, pos + 1
    i := i + 1
  }
  return i
}


showballoon(anzfinished, buchname, uanzurls = 0, ushowballoon = 0)
{
  static anzurls = 0
  static showballoon = 0
  if (uanzurls <> 0)
  {
    anzurls := uanzurls
    showballoon := ushowballoon 

  }
  if ((showballoon > 0) && (anzfinished > 0))
  {
    stringleft, buchname, buchname, 20
    text := "[" . anzfinished . "|" . anzurls . "] " . buchname . "..."
    Traytip, , %text%, , 17
    SetTimer, RemoveTrayTip, 3000
  }
  
  if (anzfinished < -2)
  {
    text := "All finished!"
    Traytip, , %text%, , 17
    SetTimer, RemoveTrayTip, 5000
    
  }
}

RemoveTrayTip:
SetTimer, RemoveTrayTip, Off
TrayTip
return
